/************************************************************************************
**  
* @copyright (c) 2015-, Technology Co., LTD. All Right Reserved.
*
*************************************************************************************/
/**
* @file     duye_cap_transfer.cpp
* @version     
* @brief      
* @author   
* @date     2013-04-09
* @note 
*
*  1. 2018-04-09 created this file
* 
*/
#include <duye_logger.h>
#include <duye_tcp_client.h>
#include <duye_udp_client.h>
#include <duye_cap_transfer.h>

// static const int8* DUYE_LOG_PREFIX = "duye.cap.transfer";

namespace duye {

ModuleMsg::ModuleMsg() : m_msgId(0), m_data(NULL), m_size(0) {}
ModuleMsg::ModuleMsg(const std::string& msgName, const uint32 msgId) 
	: m_msgId(msgId), m_msgName(msgName), m_data(NULL), m_size(0) {}
ModuleMsg::~ModuleMsg() { delete m_data; }

void ModuleMsg::setMsgId(const uint32 id) { m_msgId = id; }
uint32 ModuleMsg::getMsgId() const { return m_msgId; }
void ModuleMsg::setMsgName(const std::string& name) { m_msgName = name; }
const std::string& ModuleMsg::getMsgName() const { return m_msgName; }
void ModuleMsg::setData(const int8* data, const uint32 size) {
	delete m_data;
	m_data = new int8[size + 1]; 
	memcpy(m_data, data, size);
	m_size = size;
	m_data[m_size] = 0;
}

const int8* ModuleMsg::getData() const { return m_data; }
uint32 ModuleMsg::getDataSize() const { return m_size; }

// NetTransfer::NetTransfer() : m_client(NULL), m_remoteIP(0), m_remotePort(0)
// 	, m_localIP(0), m_localPort(0), m_isConnect(false)
// 	, m_protoType(packet::PROTO_NUKNOWN) {
// 	createClient();
// }

// NetTransfer::~NetTransfer() {
// 	close();
// 	delete m_client;
// }

// void NetTransfer::setTransferProto(const packet::ProtocolType protoType) {
// 	m_protoType = protoType;
// }

// int32 NetTransfer::send(const int8* data, const uint32 size) {
// 	if (!m_isConnect) {
// 		return -1;
// 	}

// 	int32 ret = m_client->send(data, size);
// 	if (ret == WDPI_NET_CONNECTION_OFF) {
// 		m_isConnect = false;
// 	}

// 	return ret;
// }

// int32 NetTransfer::recv(int8* recvBuf, const uint32 bufSize) {
// 	if (!m_isConnect || recvBuf == NULL) {
// 		return -1;
// 	}

// 	return m_client->recv(recvBuf, bufSize);
// }

// int32 NetTransfer::recv(int8* recvBuf, const uint32 bufSize, const uint32 timeout) {
// 	if (!m_isConnect || recvBuf == NULL) {
// 		return -1;
// 	}

// 	return m_client->recv(recvBuf, bufSize, timeout);
// }

// void NetTransfer::setRemoteAddr(const uint32 ip, const uint16 port) {
// 	m_remoteIP = ip;
// 	m_remotePort = port;
// }

// uint32 NetTransfer::getRemoteIP() const {
// 	return m_remoteIP;
// }

// void NetTransfer::setLocalAddr(const uint32 ip, const uint16 port) {
// 	m_localIP = ip;
// 	m_localPort = port;
// }

// uint32 NetTransfer::getLocalIP() const {
// 	return m_localIP;
// }

// bool NetTransfer::connect() {
// 	if (m_client == NULL) {
// 		if (!createClient()) {
// 			return false;
// 		}
// 	}

// 	if (m_isConnect) {
// 		return true;
// 	}

// 	if (m_protoType == packet::PROTO_TCP) {
// 		sys::TcpClient* tcpClient = dynamic_cast<sys::TcpClient*>(m_client);
// 		if (tcpClient != NULL) {
// 			m_isConnect = tcpClient->connect();
// 		}
// 	} else {
// 		m_isConnect = true;
// 	}

// 	return m_isConnect;
// }

// bool NetTransfer::close() {
// 	if (m_protoType == packet::PROTO_TCP) {
// 		sys::TcpClient* tcpClient = dynamic_cast<sys::TcpClient*>(m_client);
// 		if (tcpClient != NULL) {
// 			tcpClient->close();
// 		}
// 	}

// 	m_isConnect = false;

// 	return true;
// }

// bool NetTransfer::createClient() {
// 	switch (m_protoType) {
// 	case packet::PROTO_TCP:
// 		m_client = new TcpClient();
// 		break;
// 	case packet::PROTO_UDP:
// 		m_isConnect = true;
// 		m_client = new UdpClient();
// 		break;
// 	default:
// 		return false;
// 		break;
// 	}

// 	m_client->setRemoteAddr(m_remoteIP, m_remotePort);
// 	m_client->setLocalAddr(m_localIP, m_localPort);

// 	return true;
// }

ModuleTransfer::ModuleTransfer() {}
ModuleTransfer::~ModuleTransfer() {}

bool ModuleTransfer::send(const ModuleMsg& msg) {
	for (RecverList::iterator iter = m_recverList.begin(); iter != m_recverList.end(); ++iter) {
		if (*iter != NULL) {
			(*iter)->recv(msg);
		}
	}

	return true;
}

void ModuleTransfer::addRecver(RecverInterface* recver) {
	m_recverList.push_back(recver);
}

}
